package com.rskj.cashbook.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.IsoFields;
import java.time.temporal.WeekFields;
import java.util.Date;
import java.util.Locale;

public class DateUtil {

    private final static String QUARTER = "Q";
    private final static String WEEK = "W";

    /**
     * 获取周
     *
     * @param date
     * @return
     */
    public static String getWeekOfYear(Date date) {
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        WeekFields weekFields = WeekFields.of(Locale.getDefault()); // 选择你的Locale（这里是系统默认）
        int week = localDate.get(weekFields.weekOfWeekBasedYear());
        return getYear(date) + WEEK + week;
    }

    /**
     * 获取周 (LocalDateTime版本)
     *
     * @param dateTime
     * @return
     */
    public static String getWeekOfYear(LocalDateTime dateTime) {
        LocalDate localDate = dateTime.toLocalDate();
        WeekFields weekFields = WeekFields.of(Locale.getDefault());
        int week = localDate.get(weekFields.weekOfWeekBasedYear());
        return getYear(dateTime) + WEEK + week;
    }

    /**
     * 获取季度
     *
     * @param date
     * @return
     */
    public static String getQuarterOfYear(Date date) {
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        int quarter = localDate.get(IsoFields.QUARTER_OF_YEAR);
        return getYear(date) + QUARTER + quarter;
    }

    /**
     * 获取季度 (LocalDateTime版本)
     *
     * @param dateTime
     * @return
     */
    public static String getQuarterOfYear(LocalDateTime dateTime) {
        LocalDate localDate = dateTime.toLocalDate();
        int quarter = localDate.get(IsoFields.QUARTER_OF_YEAR);
        return getYear(dateTime) + QUARTER + quarter;
    }

    /**
     * 获取年份
     *
     * @param date
     * @return
     */
    public static String getYear(Date date) {
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        int year = localDate.getYear();
        return String.valueOf(year);
    }

    /**
     * 获取年份 (LocalDateTime版本)
     *
     * @param dateTime
     * @return
     */
    public static String getYear(LocalDateTime dateTime) {
        int year = dateTime.getYear();
        return String.valueOf(year);
    }

    /**
     * 获取月份
     *
     * @param date
     * @return
     */
    public static String getMonthOfYear(Date date) {
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyyMM");
        return localDate.format(monthFormatter);
    }

    /**
     * 获取月份 (LocalDateTime版本)
     *
     * @param dateTime
     * @return
     */
    public static String getMonthOfYear(LocalDateTime dateTime) {
        LocalDate localDate = dateTime.toLocalDate();
        DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyyMM");
        return localDate.format(monthFormatter);
    }

    public static void main(String[] args) {
        LocalDate localDate = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        WeekFields weekFields = WeekFields.of(Locale.getDefault()); // 选择你的Locale（这里是系统默认）
        int week = localDate.get(weekFields.weekOfWeekBasedYear());
        System.out.println(week);
    }
}
